<template>
  <div class="login_container">
    <el-row :gutter="20">
      <el-col :span="12" :xs="0">1</el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginForms"
        >
          <h1>Hello</h1>
          <h2>欢迎来到阿杰优选</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              v-model="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              class="login-btn"
              @click="login"
              :loading="loading"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import useUserStore from '@/store/modules/user'
import { useRouter } from 'vue-router'
import { ElNotification } from 'element-plus'
import { getTime } from '@/utils/time'
let useStore = useUserStore()
let $router = useRouter()
let loading = ref(false)
let loginForms = ref()
// 收集账号密码信息
let loginForm = reactive({ username: 'admin', password: '111111' })
const login = async () => {
  await loginForms.value.validate()

  loading.value = true //按钮显示加载
  try {
    await useStore.userLogin(loginForm)
    $router.push('/')
    // 成功弹窗
    ElNotification({
      type: 'success',
      message: '登陆成功',
      title: `Hi~ ${loginForm.username} ${title}`,
    })
    loading.value = false //按钮停止加载
  } catch (error) {
    loading.value = false //按钮停止加载
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    })
  }
}
let title = getTime()
// 自定义检校规则
const validatorUserName = (rule: any, value: any, callback: any) => {
  if (/^\d{5,10}$/.test(value)) {
    callback()
  } else {
    callback(new Error('帐号应为5~10位'))
  }
}
// 表单校验需要的配置
const rules = {
  username: [
    // 一个对象即为一个规则
    // { required: true, message: '用户名不能为空', trigger: 'blur' },
    {
      required: true,
      min: 5,
      max: 10,
      message: '用户名长度应为5~10位',
      trigger: 'change',
    },
    // { trigger: 'change', validator: validatorUserName }
  ],
  password: [
    {
      required: true,
      min: 6,
      max: 15,
      message: '密码长度应为6~10位',
      trigger: 'change',
    },
  ],
}
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/background.jpg') no-repeat;
  background-size: cover;
}

.login_form {
  position: relative;
  top: 30vh;
  width: 80%;
  background: url('@/assets/login_form.png') no-repeat;
  background-size: cover;

  padding: 40px;

  h1 {
    color: white;
    font-size: 40px;
  }

  h2 {
    color: white;
    font-size: 20px;
    margin: 20px 0;
  }

  .login-btn {
    width: 100%;
  }
}
</style>
